package com.wtwd.campus.h5_manager.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.h5_manager.common.Constant;
import com.wtwd.campus.h5_manager.dao.H5ManageUserMapper;
import com.wtwd.campus.h5_manager.entity.H5ManageUser;
import com.wtwd.campus.h5_manager.entity.vo.H5ManageUserVo;
import com.wtwd.campus.h5_manager.service.H5ManagerUserService;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2022-03-04
 */
@Service
public class H5ManagerUserServiceImpl implements H5ManagerUserService {

    @Autowired
    private H5ManageUserMapper h5ManageUserMapper;

    @Autowired
    private RedisService redisService;


    @Override
    public H5ManageUser getSysUserByAccount(String account) {
        return new LambdaQueryChainWrapper<>(h5ManageUserMapper)
                .eq(H5ManageUser::getAccount, account)
                .one();
    }

    @Override
    public Boolean checkLoginUser(H5ManageUser sysUser, String password) {
        if (sysUser == null) {
            return false;
        }
        if (!sysUser.getPassword().equals(password)) {
            return false;
        }
        return true;
    }

    @Override
    public String createUserToken(String flag, Integer userId, long tokenExpire, TimeUnit minutes) {
        String token = Tools.getRamdomStr();
        redisService.setKey(token, flag, tokenExpire, minutes);
        //以key为键,用户信息为value,存储用户信息
        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("current_token", token);//判断用户是否多端登录
        userMap.put("userId", String.valueOf(userId));
        redisService.setHash(flag, userMap, tokenExpire, minutes);
        return token;
    }

    @Override
    public Integer getUserIdByToken(String token) throws IllegalArgumentException {
        String key = redisService.getValue(token);
        if (key != null) {
            Map<String, Object> userMap = redisService.getHashMap(key);
            if (userMap != null) {
                String userId = (String) userMap.get("userId");
                return Integer.valueOf(userId);
            }
        }
        return null;
    }

    @Override
    public Map<String, Object> getUserMapByToken(String token) {
        String key = redisService.getValue(token);
        if (key != null) {
            return redisService.getHashMap(key);
        }
        return null;
    }

    @Override
    public H5ManageUser getUserById(Integer userId) {
        return new LambdaQueryChainWrapper<>(h5ManageUserMapper)
                .eq(H5ManageUser::getUserId, userId)
                .one();
    }

    @Override
    public Integer deleteByCustomerId(Integer customerId) {
        return h5ManageUserMapper.deleteByCustomerId(customerId);
    }

    @Override
    public Integer deleteByRoleId(Integer roleId) {
        return h5ManageUserMapper.deleteByRoleId(roleId);
    }

    @Override
    public IPage<H5ManageUser> getUserPageByCustomerId(Integer customerId, Page<H5ManageUser> page) {
        return h5ManageUserMapper.getUserPageByCustomerId(customerId, page);
    }

    @Override
    public IPage<H5ManageUserVo> getUserPageByConditions(H5ManageUser userSearch, Page<H5ManageUser> page) {
        return h5ManageUserMapper.getUserPageByConditions(userSearch, page);
    }

    @Override
    public Boolean authUserName(H5ManageUser manageUserVo) {
        H5ManageUser manageUser = this.getSysUserByAccount(manageUserVo.getAccount());
        if (manageUserVo.getUserId() == null||manageUserVo.getUserId()==0) {//新增
            if (manageUser == null) {
                return true;
            }
        } else {
            if (manageUser != null) {
                if (manageUserVo.getUserId().equals(manageUser.getUserId())) {
                    return true;
                }
            }else {
                return true;
            }
        }
        return false;
    }

    @Override
    public Integer insertUser(H5ManageUser manageUser) {
        return h5ManageUserMapper.insert(manageUser);
    }

    @Override
    public Integer updateAccount(H5ManageUser manageUserVo) {
        return h5ManageUserMapper.updateById(manageUserVo);
    }

    @Override
    public Integer deleteByUserId(Integer userId) {
        redisService.deleteKey(Constant.H5_MANAGE_USER_LOGIN_KEY_PREFIX + userId);
        return h5ManageUserMapper.deleteById(userId);
    }


}
